/*
 * Created by SharpDevelop.
 * User: fatman
 * Date: 10/27/2008
 * Time: 3:38 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;

namespace CUALib
{
	/// <summary>
	/// Description of Scrollbar.
	/// </summary>
	public enum ScrollbarTypes
	{
		Horizontal,
		Vertical
	}
	public class Scrollbar : ConsoleControl
	{

		public Scrollbar(bool consoleExists)
			: base(consoleExists)
		{
			_current=0;
			_max=10;
		}


		private ScrollbarTypes _type=ScrollbarTypes.Vertical;

		public ScrollbarTypes ScrollbarType
		{
			get { return _type; }
			set { _type = value; }
		}
		


		internal override void Redraw()
		{
			if (this.Parent == null)
				return;
			if (Parent.ContentRect == Rectangle.Empty)
				return;

			
			ConsoleColor oldForeColor = Console.ForegroundColor;
			Console.ForegroundColor = ForegroundColor;
			ConsoleColor oldBackColor = Console.BackgroundColor;
			Console.BackgroundColor = BackgroundColor;
			Point oldCursorLocation = new Point(Console.CursorLeft, Console.CursorTop);

			

			if (ScrollbarType == ScrollbarTypes.Vertical)
			{
				Size = new Size(1, Parent.Height - 2);
				Location = new Point(Parent.Width, 1);
				Rectangle ab = AbsoluteBounds;
				Console.SetCursorPosition(ab.X,ab.Y);
				
				for (int i = 0; i < ab.Height; i++)
				{
					Console.SetCursorPosition(ab.X, ab.Y + i);
					if(_max>0 && i==_current*ab.Height/(_max+1))
						Console.Write(DecorationSet[BoxDrawingDecorationNames.scrollBarVerticalKnob]);
					else
						Console.Write(DecorationSet[BoxDrawingDecorationNames.scrollBarVerticalBackground]);
				}

			}
			#region no horizontal bar yet
			/*else
			{
				Size = new Size(Parent.Width - 2, 1);
				Location = new Point(1, Parent.Height);

				Rectangle ab = AbsoluteBounds;
				string line = string.Empty;

				line = line.PadRight(ab.Width, '-');
				Console.Write(line);

			}*/
			#endregion
			
			
			Console.ForegroundColor = oldForeColor;
			Console.BackgroundColor = oldBackColor;
			Console.SetCursorPosition(oldCursorLocation.X, oldCursorLocation.Y);
		}
		
		
		private int _current=0;
		private int _max=0;
		
		public void SetPosition(int current,int max)
		{
			//Console.WriteLine("-- {0}{1}",current,max);
			_current=current;
			_max=max;
			Invalidate();
		}
		

		public override void Invalidate(System.Drawing.Rectangle rect)
		{
			if (!Visible)
				return;
			Redraw();
			//throw new Exception("The method or operation is not implemented.");
		}
		
		
		
		
		public override bool ReceiveEvent(ConsoleKeyInfo cki)
		{
			
			if(Focused)
			{
				return Parent.ReceiveEvent(cki);
			}
			return base.ReceiveEvent(cki);

			
		}
		
	}
}
